/**
 *
 */
package anysware.core.rest.resource;

import javax.activation.FileTypeMap;

import org.apache.commons.lang.StringUtils;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Resource;
import org.restlet.resource.Variant;

import anysware.restlet.RepresentationHelper;

/**
 * TODO: doc me
 *
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 111 $
 * @version
 */
public abstract class AnyswareResource extends Resource {
	protected RepresentationHelper	repHelper;

	/*
	 * (non-Javadoc)
	 *
	 * @see org.restlet.resource.Resource#init(org.restlet.Context,
	 *      org.restlet.data.Request, org.restlet.data.Response)
	 */
	@Override
	public void init(final Context context, final Request request,
	        final Response response) {
		super.init(context, request, response);
		initInner();
		extractType(request);
	}

	/**
	 * @param repHelper
	 *            the repHelper to set
	 */
	public void setRepHelper(final RepresentationHelper repHelper) {
		this.repHelper = repHelper;
	}

	protected void extractType(final Request request) {
		final String type = (String) request.getAttributes().get("type");
		if (!StringUtils.isEmpty(type)) {
			final Variant variant = new Variant(MediaType.valueOf(FileTypeMap
			        .getDefaultFileTypeMap().getContentType("file." + type)));
			getVariants().add(variant);
		}
	}

	protected abstract void initInner();

}
